package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.AttachmentEntityMapper;
import com.antler.smt.admin.mapper.MessagePushEntityMapper;
import com.antler.smt.admin.mapper.MessagePushInfoMapper;
import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.AttachmentEntity;
import com.antler.smt.entity.MessagePushEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.base.BasePageReq;
import com.antler.smt.request.portal.MessagePushReq;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.MessagePushService;
import com.antler.smt.utils.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhangyi
 * @date 2018/5/17 16:00
 */
@Service
public class MessagePushServiceImpl implements MessagePushService {


    @Autowired
    private MessagePushEntityMapper messagePushEntityMapper;

    @Autowired
    private MessagePushInfoMapper messagePushInfoMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    /**
     * 写入发布消息
     *
     * @param req
     * @return
     */
    @Override
    public Result writeMessage(MessagePushReq req) {
        Result result = null;

        //创建推送消息实体
        MessagePushEntity messagePushEntity = new MessagePushEntity();

        messagePushEntity.setType(req.getType());
        messagePushEntity.setPushContent(req.getPushContent());
        messagePushEntity.setMessageTitle(req.getMessageTitle());
        messagePushEntity.setPushImagesId(req.getPushImagesId());
        messagePushEntity.setCreateDate(DateUtil.stampToDate(System.currentTimeMillis()));
        messagePushEntity.setUpdateDate(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = messagePushEntityMapper.insertSelective(messagePushEntity);

        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     * 删除消息
     *
     * @param memssageId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result del(Long memssageId) {
        Result result = null;

        int i = messagePushEntityMapper.deleteByPrimaryKey(memssageId);

        result = (0 == i?new Result<>(ResultCodeEnum.APPEND_DATA_ERROR):new Result<>(ResultCodeEnum.OPERATION_SUCCESS));

        return result;
    }


    /**
     * 获取消息推送信息
     *
     * @return
     */
    @Override
    public Result<PageInfo<MessagePushEntity>> getMessagePushList(BasePageReq req,List<Integer> list) {
        Result result = null;

        PageHelper.startPage(req.getPageCurrent(),req.getPageSize());
        Page<MessagePushEntity> messagePushList = messagePushInfoMapper.getMessagePushList(list);

        for(int i = 0; i < messagePushList.size(); i++){
            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(messagePushList.get(i).getPushImagesId());

            if(attachmentEntity != null) {
                messagePushList.get(i).setImgName(attachmentEntity.getFileName());
            }
        }

        if(messagePushList != null){
            PageInfo listInfo = new PageInfo(messagePushList);
            result = new Result(listInfo);
        }else {
            result = new Result(ResultCodeEnum.QUERY_DATA_ERROR, "查询消息数据为空！");
        }
        return result;
    }
}
